package com.bgds.sys.data.iXCom29d.gateway.impl;

import com.cisdi.data.sdk.gateway.netty.SessionFactory;
import com.cisdi.data.sdk.gateway.netty.impl.AbstractChannelInitializer;
import com.cisdi.data.sdk.gateway.netty.impl.AbstractIoChannelHandler;
import com.cisdi.data.sdk.gateway.netty.impl.DefaultIoChannelHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @param <I>
 */
public class IXCom29dChannelInitializer<I> extends AbstractChannelInitializer<I> {
	private SessionFactory factory;
	private Charset charset;
	
	/**
	 * 构造通道初始化器对象
	 * @param factory 会话工厂
	 * byteOrder 大小端
	 *  passthroughAddress 透传地址，如果不使用透传，传null
	 *  sslConfig ssl证书配置，如果不启用tls，传null
	 */
	public IXCom29dChannelInitializer(SessionFactory factory, Charset charset) {
		this.factory = factory;
		this.charset = charset;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected AbstractIoChannelHandler<I> getTailHandler() {
		return new DefaultIoChannelHandler(factory);
	}

	@Override
	protected List<ChannelHandler> getHandlers() {
		List<ChannelHandler> handlers = new ArrayList<ChannelHandler>();
		handlers.add(new BaoXingFrameDecoder(charset));
		
		// 涉及到响应消息的，全是使用ASCII编码
		handlers.add(new BaoXingVoEncoder(CharsetUtil.US_ASCII));
		return handlers;
	}

	@Override
	protected IdleStateHandler getIdleStateHandler() {
		return new IdleStateHandler(15, 15, 30);
	}

	@Override
	protected ChannelHandler getSslChannelHandler(Channel socketChannel) {
		return null;
	}

}
